<?php
/**
 * @package JFusion_Elgg
 * @author Arunoda Susiripala
 * @copyright Copyright (C) 2008 JFusion. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */

// no direct access
defined('_JEXEC' ) or die('Restricted access' );

/**
 * This class has some methods get some parameter values from the
 * elgg database which is require in the user registration
 * @package JFusion_Elgg
 */


class dbUtil{
	/**
	 * admins user id
	 * @var int
	 */
	public $admin_guid;
	/**
	 * site id
	 * @var int
	 */
	public $site_guid;
	/**
	 * metadata string id of admin_created
	 * which is the normal-usertype
	 * @var int
	 */
	public $meta_admin_created;
	
	/**
	 * metadata string id of admin
	 * which is the admin-usertype
	 * @var int
	 */
	public $meta_admin;
	
	public function init(){
		$db = JFusionFactory::getDatabase("elgg");
		$this->loadMetaAdminCreated($db);
		$this->loadMetaAdmin($db);
		$this->loadSiteGuid($db);
		$this->loadAdminGuid($db);
	}
	
	private function loadAdminGuid($db){
		$sql="SELECT entity_guid from #__metadata WHERE name_id=".$this->meta_admin;
		$db->setQuery($sql);
		$res=$db->loadResultArray();
		if(sizeof($res>0)){
			$this->admin_guid=$res[0];
		}
	}
	
	private function loadSiteGuid($db){
		$sql="SELECT guid from #__sites_entity";
		$db->setQuery($sql);
		$res=$db->loadResultArray();
		if(sizeof($res>0)){
			$this->site_guid=$res[0];
		}
	}
	
	private function loadMetaAdminCreated($db){
		$sql="SELECT id from #__metastrings WHERE string='admin_created'" ;
		$db->setQuery($sql);
		$this->meta_admin_created=$db->loadResult();
	}
	
	private function loadMetaAdmin($db){
		$sql="SELECT id from #__metastrings WHERE string='admin'" ;
		$db->setQuery($sql);
		$this->meta_admin=$db->loadResult();
	}
}

$dbutil=new dbUtil();
$dbutil->init();
echo "admin:" . $dbutil->admin_guid ."<br>";
echo "meta admin:" . $dbutil->meta_admin ."<br>";
echo "meta admin_created:" . $dbutil->meta_admin_created ."<br>";
echo "site:" . $dbutil->site_guid ."<br>";

?>